package com.gollum.config;

import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.generator.ImageCaptchaGenerator;
import cloud.tianai.captcha.generator.ImageTransform;
import cloud.tianai.captcha.generator.common.constant.SliderCaptchaConstant;
import cloud.tianai.captcha.generator.impl.CacheImageCaptchaGenerator;
import cloud.tianai.captcha.generator.impl.MultiImageCaptchaGenerator;
import cloud.tianai.captcha.generator.impl.StandardSliderImageCaptchaGenerator;
import cloud.tianai.captcha.generator.impl.transform.Base64ImageTransform;
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
import cloud.tianai.captcha.resource.ResourceStore;
import cloud.tianai.captcha.resource.common.model.dto.Resource;
import cloud.tianai.captcha.resource.impl.DefaultImageCaptchaResourceManager;
import cloud.tianai.captcha.resource.impl.DefaultResourceStore;
import cloud.tianai.captcha.resource.impl.provider.ClassPathResourceProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static cloud.tianai.captcha.generator.impl.StandardSliderImageCaptchaGenerator.DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH;

/**
 * 验证码资源加载
 *
 * @author Herther
 * @version 1.0.0
 * @createTime 2022年08月28日 20:38:00
 */
@Configuration
public class CaptchaConfig {

    private final String CLASSPATH = "classpath";

    /** 默认提前缓存多少个 **/
    private final Integer DEFAULT_SIZE = 10;
    /** 出错后 等待xx时间再进行生成 **/
    private final Integer WAIT_TIME = 1000;
    /** 检查时间间隔 **/
    private final Integer PERIOD = 100;

    /**
     * 启动时懒加载图片资源
     *
     * @auther: Herther
     * @version 1.0.0
     * @date: 2022/8/28 21:48
     */
    @Bean
    public ImageCaptchaGenerator imageCaptchaGenerator(){
        ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
        ImageTransform imageTransform = new Base64ImageTransform();
        //设置图片资源
        ResourceStore resourceStore = imageCaptchaResourceManager.getResourceStore();
        setTemplate(resourceStore);
        setResourceStore(resourceStore);
        //带缓存 提前生成
        ImageCaptchaGenerator imageCaptchaGenerator =
                new CacheImageCaptchaGenerator(new MultiImageCaptchaGenerator(imageCaptchaResourceManager,imageTransform), DEFAULT_SIZE, WAIT_TIME, PERIOD).init(false);
        return imageCaptchaGenerator;
    }



    /**
     *加载验证码资源
     *
     * @auther: Herther
     * @version 1.0.0
     * @date: 2022/8/28 20:41
     */
    @SuppressWarnings("all")
    private void setResourceStore(ResourceStore resourceStore){
        // 模板
        //setTemplate();
        // 添加自定义背景图片
        // 添加滑块 图片
        for(int i = 1; i <= 6; i++){
            StringBuilder path =
                    new StringBuilder("captcha/20220828/").append("0").append(i).append(".jpg");
            resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(CLASSPATH, path.toString()));
        }
        //添加 旋转图片资源
        for(int i = 7; i <= 11; i++){
            StringBuilder path =
                    new StringBuilder("captcha/20220828/");
            if (i<=10){
                path.append(i).append(".jpg");
            }else{
                path.append("0").append(i).append(".jpg");
            }
            resourceStore.addResource(CaptchaTypeConstant.ROTATE, new Resource(CLASSPATH, path.toString()));
        }
        // TODO: 2022/8/28 后期添加图片点选资源 与 concat
        //addResource(CaptchaTypeConstant.CONCAT, new Resource(CLASSPATH, "bgimages/48.jpg"));
        //addResource(CaptchaTypeConstant.WORD_IMAGE_CLICK, new Resource(CLASSPATH, "bgimages/c.jpg"));

    }

    /**
     * 设置验证码模板
     *
     * @param
     * @return
     * @auther: Herther
     * @version 1.0.0
     * @date: 2022/8/28 21:16
     */
    private void setTemplate(ResourceStore resourceStore){
        // 滑块验证码 模板 (系统内置)
        Map<String, Resource> sliderTemplate1 = new HashMap<>(4);
        sliderTemplate1.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/1/active.png")));
        sliderTemplate1.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/1/fixed.png")));
        sliderTemplate1.put(SliderCaptchaConstant.TEMPLATE_MATRIX_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/1/matrix.png")));
        Map<String, Resource> sliderTemplate2 = new HashMap<>(4);
        sliderTemplate2.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/2/active.png")));
        sliderTemplate2.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/2/fixed.png")));
        sliderTemplate2.put(SliderCaptchaConstant.TEMPLATE_MATRIX_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/2/matrix.png")));
        // 旋转验证码 模板 (系统内置)
        Map<String, Resource> rotateTemplate = new HashMap<>(4);
        rotateTemplate.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, StandardSliderImageCaptchaGenerator.DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/3/active.png")));
        rotateTemplate.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, StandardSliderImageCaptchaGenerator.DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/3/fixed.png")));
        rotateTemplate.put(SliderCaptchaConstant.TEMPLATE_MATRIX_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, StandardSliderImageCaptchaGenerator.DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/3/matrix.png")));

        // 1. 添加一些模板
        resourceStore.addTemplate(CaptchaTypeConstant.SLIDER, sliderTemplate1);
        resourceStore.addTemplate(CaptchaTypeConstant.SLIDER, sliderTemplate2);
        resourceStore.addTemplate(CaptchaTypeConstant.ROTATE, rotateTemplate);
    }




}
